﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace fap   
{
    /// <summary>
    /// 遍历文件夹内信息
    /// </summary>
    public static class File_Directory
    {

        /// <summary>
        /// 获取指定文件夹下 当前目录及所有子目录文件夹 路径
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>所有文件夹路径</returns>
        public static string[] Read_AllDirPaths(string filePath)
        {

            string[] directorys = Directory.GetDirectories(filePath, "*", SearchOption.AllDirectories);
            return directorys;
        }
        /// <summary>
        /// 获取指定文件夹下当前目录 文件夹 路径
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件夹路径</returns>
        public static string[] Read_DirPaths(string filePath)
        {

            string[] directorys = Directory.GetDirectories(filePath, "*", SearchOption.TopDirectoryOnly);
            return directorys;
        }


        /// <summary>
        /// 获取指定文件夹下 当前目录及所有子目录文件 路径
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>所有文件路径</returns>
        public static string[] Read_AllFilPaths(string filePath)
        {
            string[] files = Directory.GetFiles(filePath, "*.*", SearchOption.AllDirectories);
            return files;

        }


        /// <summary>
        /// 获取指定文件夹下当前目录 文件 路径
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件路径</returns>
        public static string[] Read_FilPaths(string filePath)
        {
            string[] files = Directory.GetFiles(filePath, "*.*", SearchOption.TopDirectoryOnly);
            return files;

        }


        /// <summary>
        /// 获取指定文件夹下当前目录 文件 路径
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件夹下文件名</returns>
        public static string[] Read_FilNames(string filePath)
        {
            List<string> arr = new List<string>();
            DirectoryInfo dir = new DirectoryInfo(filePath);
            string name;
            //遍历文件夹
            foreach (FileInfo file in dir.GetFiles())
            {
                name = Path.GetFileNameWithoutExtension(file.Name);
                arr.Add(name);
            }
            string[] str = new string[arr.Count];
            for (int i = 0; i < arr.Count; i++)
            {
                str[i] = arr[i].ToString();
            }
            return str;

        }

        /// <summary>
        /// 获取指定文件夹下当前目录 文件 路径
        /// </summary>
        /// <param name="Path">文件路径</param>
        /// <returns>文件夹下文件名</returns>
        public static string[] Read_DirectoryNames(string Path)
        {
            List<string> arr = new List<string>();
            DirectoryInfo dir = new DirectoryInfo(Path);
            string name;

            foreach (DirectoryInfo directoryName in dir.GetDirectories())
            {
                name = directoryName.Name;
                arr.Add(name);
            }

            string[] str = new string[arr.Count];
            for (int i = 0; i < arr.Count; i++)
            {
                str[i] = arr[i].ToString();
            }
            return str;

        }



        /// <summary>
        /// 复制文件夹下的所有文件、目录到指定的文件夹
        /// </summary>
        /// <param name="dir">源文件夹地址</param>
        /// <param name="desDir">指定的文件夹地址</param>
        public static void CopyFileAndDir(string dir, string desDir)
        {
            if (!System.IO.Directory.Exists(desDir))
            {
                System.IO.Directory.CreateDirectory(desDir);
            }
            IEnumerable<string> files = System.IO.Directory.EnumerateFileSystemEntries(dir);
            if (files != null && files.Count() > 0)
            {
                foreach (var item in files)
                {
                    string desPath = System.IO.Path.Combine(desDir, System.IO.Path.GetFileName(item));

                    //如果是文件
                    var fileExist = System.IO.File.Exists(item);
                    if (fileExist)
                    {
                        //复制文件到指定目录下                     
                        System.IO.File.Copy(item, desPath, true);
                        continue;
                    }

                    //如果是文件夹                   
                    CopyFileAndDir(item, desPath);

                }
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fileFullPath"></param>
        public static void DelectFile(string fileFullPath)
        {
          try
            {
                // 2、根据路径字符串判断是文件还是文件夹
                FileAttributes attr = File.GetAttributes(fileFullPath);
                // 3、根据具体类型进行删除
                if (attr == FileAttributes.Directory)
                {
                    // 3.1、删除文件夹
                    Directory.Delete(fileFullPath, true);
                }
                else
                {
                    // 3.2、删除文件
                    File.Delete(fileFullPath);
                }
                File.Delete(fileFullPath);
            }
            catch
            {

            }
                
            

        }
    }
}
